/*
 * TinyXml2 的使用
 */
#include <QCoreApplication>
#include <QDebug>
#include "tinyxml2.h"


const QString g_reqXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                         "<Req>"
                         "<CmdType>11</CmdType>"
                         "<ArrayOfDir>"
                         "<Director>BloodAll1</Director>"
                         "<Director>BloodAll2</Director>"
                         "<Director>BloodAll3</Director>"
                         "</ArrayOfDir>"
                         "</Req>";

const QString g_ackXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                         "<Ack>"
                         "<CmdType>11</CmdType>"
                         "<Result>0</Result>"
                         "<ErrorMsg>分离机磁盘不足</ErrorMsg>"
                         "</Ack>";

struct AddDirReq {
    int cmdType;
    QVector<QString> dirs;
};

struct AddDirAck {
    int cmdType;
    int result;
    QString errorMsg;
};


int main(int argc, char *argv[])
{
    AddDirReq req;
    AddDirAck ack;
    using namespace tinyxml2;
    XMLDocument doc;
    // 解析Req
    XMLError err = doc.Parse(g_reqXml.toStdString().c_str());
    if (err == XMLError::XML_SUCCESS) {
        XMLElement *reqElement =  doc.RootElement();
        if (reqElement == nullptr) {
            qDebug() << "root element is nullptr";
            return 0;
        }
        XMLElement *cmdElement = reqElement->FirstChildElement();
        if (cmdElement == nullptr) {
            qDebug() << "first element is nullptr";
            return 0;
        }
        QString cmdTypeValue(cmdElement->Value());
        qDebug() << "Node name: " << QString(cmdElement->Name())
                 << "  value: " << QString(cmdElement->GetText());
        req.cmdType = QString(cmdElement->GetText()).toInt();

        XMLElement *arrayOfDir = cmdElement->NextSiblingElement("ArrayOfDir");
        if (arrayOfDir == nullptr) {
            qDebug() << "arrayOfDir element is nullptr";
            return 0;
        }
        qDebug() << "Node name: " << arrayOfDir->Name();
        XMLElement *directorElement = arrayOfDir->FirstChildElement();
        while (directorElement != nullptr) {
            qDebug() << "Node name: " << "  Value: " << directorElement->GetText();
            req.dirs.append(directorElement->GetText());
            directorElement = directorElement->NextSiblingElement();
        }
        doc.Clear();
        qDebug() << req.cmdType << " : " << req.dirs;

    } else {
        qDebug() << "Parse error: " << err;
    }

    // 封装XML
//const QString g_reqXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
//                         "<Req>"
//                         "<CmdType>11</CmdType>"
//                         "<ArrayOfDir>"
//                         "<Director>BloodAll1</Director>"
//                         "<Director>BloodAll2</Director>"
//                         "<Director>BloodAll3</Director>"
//                         "</ArrayOfDir>"
//                         "</Req>";
    qDebug() << "----------------------------------------------";
    // 增加xml声明
    doc.Parse("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    // 增加root标签
    XMLElement *root = doc.NewElement("Req");
    doc.InsertEndChild(root);

    // 增加CmdType标签
    XMLElement *cmdType = doc.NewElement("CmdType");
    cmdType->SetText(req.cmdType);
    root->InsertEndChild(cmdType);

    // 增加ArrayOfDir目录
    XMLElement *arrayOfDir = doc.NewElement("ArrayOfDir");
    for (QString &dir: req.dirs) {
        XMLElement *dirElement = doc.NewElement("Director");
        dirElement->SetText(dir.toStdString().c_str());
        arrayOfDir->InsertEndChild(dirElement);
    }
    root->InsertEndChild(arrayOfDir);
    // 打印
    XMLPrinter printer;
    doc.Print(&printer);
    qDebug() << printer.CStr();
	doc.Clear();
	doc.Clear();

    return 0;
}
